package com.jodi.multi.controller;

import com.jodi.multi.config.MqttClientConnect;
import com.jodi.multi.entity.MqttInfo;
import com.jodi.multi.service.MqttInfoService;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName: MqttController
 * @Description: 测试控制层
 * @Date: 2021/9/26 16:30
 */
@RestController
public class MqttController {

    @Autowired
    private MqttInfoService mqttInfoService;

    @Resource
    private MqttClientConnect mqttClientConnect;

    @PostMapping("/insert")
    public String insert(@RequestBody MqttInfo mqttInfo) throws MqttException {
        return "" + mqttInfoService.insert(mqttInfo);
    }

    @PostMapping("/testPublic")
    public String testPublic(@RequestBody MqttInfo mqttInfo) throws MqttException {
        MqttClientConnect.mqttClients.get("1").pub("/jodi/test","{\"direct\":\"0\"}");
        MqttClientConnect.mqttClients.get("1").pub("/jodi/test","{\"direct\":\"0\"}");
        MqttClientConnect.mqttClients.get("2").pub("/test2/test22/user2/get2","{\"code\":\"202\"}");

        // #
        MqttClientConnect.mqttClients.get("3").pub("/test/p1/p2","{\"code\":\"202\"}");
        MqttClientConnect.mqttClients.get("3").pub("/test/p1/p3","{\"code\":\"202\"}");
        MqttClientConnect.mqttClients.get("3").pub("/test/p2/p3","{\"code\":\"202\"}");
        return "";
    }

    @PostMapping("/remove/{id}")
    public String insert(@PathVariable String id) throws MqttException {
        return "" + mqttInfoService.remove(id);
    }
}
